define(function(require){
	var main =require('/static/main.js');
    require('/static/ajaxfileupload.js');
	var m = new main();
	function Cont(){
		this.Fun._init();
	}
	Cont.prototype.Fun = {
		_init:function(){
			// this.ajaxUpload('MainSrc');

		    this.getlist();
			this.bindEvent();
			this.ajaxUpload();
		},
		bindEvent:function(){
			
		},
		getlist:function(){
	        var param = {};
	        var sort = 0;
	        var table1 = $('#datatable').dataTable({
	        	'ordering':false,
	        	"serverSide": true,
	        	"searching":false,
	        	'bLengthChange': false,
	        	"scrollX": true,
	        	ajax: function (data, callback, settings) {
	                param.ActWheellId = $_GET['id'];
	                param.type = 100;
	                param.PageSize = data.length;
	                param.start = data.start;
	                param.PageNo = (data.start / data.length)+1;
	                $.ajax({
	                    type: "POST",
	                    url: "/drainage/turnplategiftget-list",
	                    cache: false,  
	                    data: param, 
	                    dataType: "json",
	                    success: function (result) {
                    	    var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = result.total;
                            returnData.recordsFiltered = result.total;
                            returnData.data = result.data;
                            callback(returnData);
	                    }
	                });
	            },
				"aoColumns":[
					{
						"mDataProp": 'PrizeId',
						render:function(data){
							sort ++;
                    		return sort;
                    	}
					}, 
					{
						"mDataProp": 'PrizeSrc',
						"fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
							$(nTd).html('<img src="'+oData.ImgHost+sData+'" onerror="this.src=\'/modules/img/default.jpg\'" style="width:50px;max-height:50px;">')
			            }
					},
					{
						"mDataProp": 'PrizeName',
					},
                    {
                    	"mDataProp": 'PrizeType',
                    	render:function(data){
                    		if(data == 100){
                    			return '红包'
                    		}else if(data == 200){
                    			return '思和会颜值'
                    		}else if(data == 300){
                    			return '思和会会员'
                    		}else if(data == 400){
                    			return '实物'
                    		}else{
                    			return '谢谢惠顾'
                    		}
                    	}

                   	}, 
                   	{
                    	"mDataProp": 'DeliveryType',
                    	render:function(data){
                    		if(data == 100){
                    			return '系统自动处理（虚拟产品）'
                    		}else if(data == 200){
                    			return '线下门店自提（实物产品）'
                    		}else{
                    			return '线上物流发货（实物产品）'
                    		}
                    	}
                   	},
                   	{
                    	"mDataProp": 'Probability',
                    	render:function(data){
                    		return data+'%'
                    	}
                   	},
                    {
                    	"mDataProp": 'CreateTime',
                    	render:function(data){
                    		if(data){
                    			return m.formatTimeAll2(data)
                    		}else{
                    			return '';
                    		}
                    	}
                	},

				],
	        }).api();
			// 点击搜索
	        $('#btnSearch').click(function(){
	        	param.PrizeName = $('.Name').val();
	        	sort = 0;
                table1.ajax.reload();
	        })
	        // 点击重置按钮
	        $('#btnRefresh').click(function(){
	        	$('.Name').val('');
	        	sort = 0;
	        	param.PrizeName = '';
                table1.ajax.reload();
	        })
	        // 点击删除
	        $(document).on('click','.btn_del',function(){
	        	var id = $(this).attr('id');
	        	layer.confirm('是否删除该活动？', {
				  btn: ['确定','取消'] //按钮
				}, function(){
				  	$.ajax({
	                    type: "POST",
	                    url:'/drainage/openbox-del',
	                    data:{PrizeId:id},
	                    dataType: "json",
	                    success: function (ret) {
	                	  	if(ret.IsBizSuccess){
				                layer.msg('删除成功');
				                sort = 0;
				                table1.ajax.reload();
	                	  	}else{
	                	  		layer.msg(ret.ErrMsg);
	                	  		return false;
	                	  	}
	                    }
	                });
				});
	        })
	        // 点击添加多个奖品
	        var str = '';
	        	str+='<div class="modal-body" style="padding-top: 0px;">\
                    <div class="profile-widget text-center curstome_userdetial">\
                        <div class="row">\
                            <div class="col-md-12">\
                                <div class="form-group tit">\
                                    <label for="field-3" class="control-label">奖品名称</label>\
                                    <input type="text" class="form-control PrizeNames"  placeholder="请填奖品名称" >\
                                    <div class="del-btn"><span class="fa fa-trash-o"></span></div>\
                                </div>\
                            </div>\
                        </div>\
                        <div class="row">\
                            <div class="col-md-12">\
                                <div class="form-group">\
                                    <label for="field-3" class="control-label">奖品图片</label>\
                                    <div>\
                                        <div class="form-group input-group" style="width: 100%;">\
                                            <input type="text" id="example-input2-group1" name="example-input2-group1" class="form-control fileupload" placeholder="上传奖品图片" readonly>\
                                            <span class="input-group-addon" style="height: 34px;line-height: 34px;text-align: center;padding: 0px;width: 34px;"><i class="fa ion-image"></i></span>\
                                        </div>\
                                        <div class="MainImg MainSrcImg"><img src="/modules/img/default.jpg"></div>\
                                    </div>\
                                </div>\
                            </div>\
                        </div>\
                        <div class="row">\
                            <div class="col-md-12">\
                                <div class="form-group">\
                                    <label for="field-3" class="control-label">奖品类型</label>\
                                    <select class="form-control PrizeType" >\
                                        <option value="0">谢谢惠顾</option>\
                                        <option value="100">红包</option>\
                                        <option value="200">思和会颜值</option>\
                                        <option value="300">思和会会员</option>\
                                        <option value="400">实物</option>\
                                    </select>\
                                </div>\
                            </div>\
                        </div>\
                        <div class="row">\
                            <div class="col-md-12">\
                                <div class="form-group">\
                                    <label for="field-3" class="control-label">派送方式</label>\
                                    <select class="form-control DeliveryType" >\
                                        <option value="100">系统自动处理（虚拟产品）</option>\
                                        <option value="200">线下门店自提（实物产品）</option>\
                                        <option value="300">线上物流发货（实物产品）</option>\
                                    </select>\
                                </div>\
                            </div>\
                        </div>\
                        <div class="row">\
                            <div class="col-md-12">\
                                <div class="form-group">\
                                    <label for="field-3" class="control-label">设置中奖概率<i class="per_val">1%</i></label>\
                                    <input type="range" value="1" max="100" min="1" step="1"  class="form-control trackBar">\
                                </div>\
                            </div>\
                        </div>\
                    </div>\
                </div>'
	        $('.addMoregift').click(function(){
	        	$('.bigCont').append(str);
	        })
	        // 创建
	        var _this = this;
	        $('.create_userinfo').click(function(){
	        	var PrizeList = [];
	        	var num = 0;
	        	$('.modal-body').each(function(){
	        		var data = {
		        		PrizeName: $.trim($(this).find('.PrizeNames').val()),
		        		PrizeSrc:$(this).find('.MainSrcImg img').attr('data-src'),
		        		// PrizeValue: $(this).find('.PrizeValue').val(),
		        		PrizeType: $(this).find('.PrizeType option:selected').val(),
		        		DeliveryType:$(this).find('.DeliveryType option:selected').val(),
		        		Probability: parseInt($(this).find('.trackBar').val())
		        	}
		        	if(data.PrizeName == ''){
	        			$('.pass-msg').html('奖品名称不能为空').show()
		        		return false;
		        	}
		        	if(data.PrizeSrc == ''){
		        		$('.pass-msg').html('奖品图片不能为空').show()
		        		return false;
		        	}
		        	// if(data.PrizeValue == ''){
		        	// 	$('.pass-msg').html('奖品值不能为空').show()
		        	// 	return false;
		        	// }
		        	PrizeList.push(data)
	        	})

				for(var i = 0;i < PrizeList.length;i++)
				{
				 	num += PrizeList[i].Probability
				} 
				if(num != 100){
					layer.msg('中奖概率之和必须为100%');
					return false;
				}
	        	
	        	var obj = {
	        		ActWheellId: $_GET['id'],
	        		PrizeList: PrizeList
	        	}
	        	$.ajax({
                    type: "POST",
                    url:'/drainage/turnplategiftcreate',
                    data:obj,
                    dataType: "json",
                    success: function (ret) {
                	  	if(ret.IsBizSuccess){
			                layer.msg('创建成功');
			                sort = 0;
			                $('#con-close-modal').modal('hide');
			                table1.ajax.reload();
                	  	}else{
                	  		layer.msg(ret.ErrMsg);
                	  		return false;
                	  	}
                    }
                });
	        })
	        // 删除
	        $(document).on('click','.del-btn',function(){
	        	if($('.modal-body').length  <= 1){
	        			layer.msg('至少要有一个奖品');
                        return false;
	        	}
	        	$(this).parents('.modal-body').remove();
	        })
	        // 点击添加的按钮 如果有内容就给他赋值
	        $('#addToTable').click(function(){
	        	$.ajax({
	                type: "POST",
	                url:'/drainage/turnplategiftget-list',
	                data:{ActWheellId:$_GET['id'],type:200},
	                dataType: "json",
	                success: function (ret) {
	                	if(ret.data.length > 0){
	                		var str = '';
	                		var select0,select100,select200,select300,select400;
	                		var s100,s200,s300;
		                	$.each(ret.data,function(k,v){
		                		str+='<div class="modal-body" style="padding-top: 0px;">\
					                    <div class="profile-widget text-center curstome_userdetial">\
					                        <div class="row">\
					                            <div class="col-md-12">\
					                                <div class="form-group tit">\
					                                    <label for="field-3" class="control-label">奖品名称</label>\
					                                    <input type="text" class="form-control PrizeNames"  placeholder="请填奖品名称" value="'+v.PrizeName+'" >\
					                                    <div class="del-btn"><span class="fa fa-trash-o"></span></div>\
					                                </div>\
					                            </div>\
					                        </div>\
					                        <div class="row">\
					                            <div class="col-md-12">\
					                                <div class="form-group">\
					                                    <label for="field-3" class="control-label">奖品图片</label>\
					                                    <div>\
					                                        <div class="form-group input-group" style="width: 100%;">\
					                                            <input type="text" id="example-input2-group1" name="example-input2-group1" class="form-control fileupload" placeholder="上传奖品图片" readonly>\
					                                            <span class="input-group-addon" style="height: 34px;line-height: 34px;text-align: center;padding: 0px;width: 34px;"><i class="fa ion-image"></i></span>\
					                                        </div>\
					                                        <div class="MainImg MainSrcImg"><img src="'+v.ImgHost+v.PrizeSrc+'" data-src="'+v.PrizeSrc+'" ></div>\
					                                    </div>\
					                                </div>\
					                            </div>\
					                        </div>\
					                        <div class="row">\
					                            <div class="col-md-12">\
					                                <div class="form-group">\
					                                    <label for="field-3" class="control-label">奖品类型</label>\
					                                    <select class="form-control PrizeType" >'
					                                    	if(v.PrizeType == 0){
					                                    		select0 = 'selected="true"';
					                                    		select100 =''
					                                    		select200 =''
					                                    		select300 =''
					                                    		select400 =''
					                                    	}else if(v.PrizeType == 100){
					                                    		select100 = 'selected="true"';
					                                    		select0 =''
					                                    		select200 =''
					                                    		select300 =''
					                                    		select400 =''
					                                    	}else if(v.PrizeType == 200){
					                                    		select200 = 'selected="true"';
					                                    		select0 =''
					                                    		select100 =''
					                                    		select300 =''
					                                    		select400 =''
					                                    	}else if(v.PrizeType == 300){
					                                    		select300 = 'selected="true"';
					                                    		select0 =''
					                                    		select100 =''
					                                    		select200 =''
					                                    		select400 =''
					                                    	}else if(v.PrizeType == 400){
					                                    		select400 = 'selected="true"';
					                                    		select0 =''
					                                    		select100 =''
					                                    		select200 =''
					                                    		select300 =''
					                                    	}
					                                        str+='<option value="0" '+select0+'>谢谢惠顾</option>'
					                                        // str+='<option value="100" '+select100+'>红包</option>\
					                                        // <option value="200" '+select200+'>思和会颜值</option>\
					                                        // <option value="300" '+select300+'>思和会会员</option>'
					                                        str+='<option value="400" '+select400+'>实物</option>'
					                                    str+='</select>\
					                                </div>\
					                            </div>\
					                        </div>\
					                        <div class="row">\
					                            <div class="col-md-12">\
					                                <div class="form-group">\
					                                    <label for="field-3" class="control-label">派送方式</label>\
					                                    <select class="form-control DeliveryType" >'
					                                    	if(v.PrizeType == 100){
					                                    		s100 = 'selected="true"';
					                                    		s200 =''
					                                    		s300 =''
					                                    	}else if(v.PrizeType == 200){
					                                    		s200 = 'selected="true"';
					                                    		s100 =''
					                                    		s300 =''
					                                    	}else if(v.PrizeType == 300){
					                                    		s300 = 'selected="true"';
					                                    		s100 =''
					                                    		s200 =''
					                                    	}
					                                        str+='<option value="100" '+s100+'>系统自动处理（虚拟产品）</option>\
					                                        <option value="200" '+s200+'>线下门店自提（实物产品）</option>\
					                                        <option value="300" '+s300+'>线上物流发货（实物产品）</option>'
					                                    str+='</select>\
					                                </div>\
					                            </div>\
					                        </div>\
					                        <div class="row">\
					                            <div class="col-md-12">\
					                                <div class="form-group">\
					                                    <label for="field-3" class="control-label">设置中奖概率<i class="per_val">'+v.Probability+'%</i></label>\
					                                    <input type="range" value="'+v.Probability+'" max="100" min="1" step="1"  class="form-control trackBar">\
					                                </div>\
					                            </div>\
					                        </div>\
					                    </div>\
					                </div>'
		                	})
							$('.bigCont').html(str);
	                	}
	                }
            	})
	        })
		},
		
		//上传图片
        ajaxUpload:function(){
            var obj = '';
            $(document).on("click", ".fileupload", function(){
                $("#file").click();
                obj = $(this);
            });
            //图片上传
            $("#file").on("change",function () {
                ajaxFileUpload();
            });
            function ajaxFileUpload(){
                $.ajaxFileUpload({
                    url:m.baseUrl+"/customer/edit-head",
                    secureuri: false,
                    fileElementId: 'file',
                    dataType: 'json',
                    success: function (ret) {
                        if(ret.IsBizSuccess){
                            obj.parents('.modal-body').find('.MainSrcImg img').attr({'src':ret.Photo,'data-src':ret.FileName});
                        }else{
                            layer.msg(ret.ErrMsg);
                            return false;
                        }
                    }
                })
                return false;
            }
        },

	}
	var a = new Cont();

})